package main

import (
	"time"

	"gitee.com/jabberwocky238/proj-backend/controller"
	"gitee.com/jabberwocky238/proj-backend/model"

	// "gitee.com/jabberwocky238/proj-backend/service"
	// "gitee.com/jabberwocky238/proj-backend/middleware"
	"gitee.com/jabberwocky238/proj-backend/util"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

func main() {
	// service.COSInit()
	model.GetDB()
	defer model.CloseDB()
	util.InitLogger()

	r := gin.Default()
	gin.SetMode(gin.DebugMode)

	// 配置 CORS 中间件
	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"},                                       // 允许的来源
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, // 允许的 HTTP 方法
		AllowHeaders:     []string{"Origin", "Content-Type", "Authorization"}, // 允许的请求头
		ExposeHeaders:    []string{"Content-Length"},                          // 暴露的响应头
		AllowCredentials: true,                                                // 是否允许携带凭证（如 Cookies）
		MaxAge:           12 * time.Hour,                                      // 预检请求的缓存时间
	}))
	// r.Use(cors.Default())

	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	// 用户相关路由
	// r.POST("/music/upload", controller.MusicUpload)
	r.GET("/music/exists", controller.MusicExists)
	r.GET("/music/blob", controller.MusicBlob)
	r.GET("/music/all", controller.MusicAll)
	r.GET("/music/cover", controller.MusicCover)
	r.GET("/music/metadata", controller.MusicMetaData)
	// r.GET("/music/playlist", controller.MusicPlaylist)
	// r.GET("/music/uploadPresign", controller.MusicPresignedUploadURL)
	// r.POST("/music/uploadFinished", controller.MusicPresignedUploadFinished)

	// r.GET("/user/new", controller.UserNew)

	// 身份认证相关路由
	// r.POST("/login", func(c *gin.Context) {
	// 	var login struct {
	// 		Name string `json:"name"`
	// 	}
	// 	if err := c.ShouldBindJSON(&login); err != nil {
	// 		c.JSON(400, gin.H{"error": err.Error()})
	// 		return
	// 	}
	// 	token, err := middleware.GenerateToken(login.Name)
	// 	if err != nil {
	// 		c.JSON(500, gin.H{"error": "Failed to generate token"})
	// 		return
	// 	}
	// 	c.JSON(200, gin.H{"token": token})
	// })

	// // 受保护的路由
	// protected := r.Group("/protected")
	// protected.Use(middleware.AuthMiddleware())
	// {
	// 	protected.GET("/users", controller.GetUsers)
	// }

	r.Run("127.0.0.1:23891")
}
